package com.sks.web.action.news;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.sks.bean.PageView;
import com.sks.bean.QueryResult;
import com.sks.bean.pojo.News;
import com.sks.service.news.NewsService;
import com.sks.web.action.BasicAction;
import com.sks.web.formbean.news.NewsForm;


/**
 * 公共信息信息方法
 * @author yuanjiayuan
 *
 */
public class NewsBaseAction extends BasicAction{

	/***
	 * 最新新闻
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param newsService
	 * @return
	 * @throws Exception
	 */
	public PageView<News> lastNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,NewsService newsService)
			throws Exception {
		
		NewsForm formbean = (NewsForm) form;		
		StringBuffer jpql = new StringBuffer("");
		jpql.append(" o.status = 'published'");
		List<Object> params = new ArrayList<Object>();
		PageView<News> pageView = new PageView<News>(20, formbean.getPage());
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("newsId", "desc");
		QueryResult<News> qr = newsService.getScrollData(pageView
				.getFirstResult(), pageView.getMaxresult(), jpql.toString(),
				params.toArray(), orderby);
		pageView.setQueryResult(qr);
		return pageView;
	}
	
	/***
	 * 推荐热点新闻
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param newsService
	 * @return
	 * @throws Exception
	 */
	public PageView<News> hotNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,NewsService newsService)
			throws Exception {
		

		NewsForm formbean = (NewsForm) form;		
		StringBuffer jpql = new StringBuffer("");
		jpql.append(" o.status = 'published'");
		List<Object> params = new ArrayList<Object>();
		PageView<News> pageView = new PageView<News>(20, formbean.getPage());
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("click", "desc");
		QueryResult<News> qr = newsService.getScrollData(pageView
				.getFirstResult(), pageView.getMaxresult(), jpql.toString(),
				params.toArray(), orderby);
		pageView.setQueryResult(qr);
		return pageView;
	}

	/***
	 * 热点新闻
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param newsService
	 * @return
	 */
	public PageView<News> hotsNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			NewsService newsService) {
		NewsForm formbean = (NewsForm) form;		
		StringBuffer jpql = new StringBuffer("");
		jpql.append(" o.feature.featureId = 1 and o.status = 'published'");
		List<Object> params = new ArrayList<Object>();
		
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("newsId", "desc");
		PageView<News> pageView = new PageView<News>(20, formbean.getPage());
		QueryResult<News> qr = newsService.getScrollData(pageView
				.getFirstResult(), pageView.getMaxresult(), jpql.toString(),
				params.toArray(), orderby);
		pageView.setQueryResult(qr);
		return pageView;
	}

	/***
	 * 房产市场资讯
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param newsService
	 * @return
	 */
	public PageView<News> houseNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			NewsService newsService) {
		NewsForm formbean = (NewsForm) form;		
		StringBuffer jpql = new StringBuffer("");
		jpql.append(" o.feature.featureId = 2 and o.status = 'published'");
		List<Object> params = new ArrayList<Object>();
		
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("newsId", "desc");
		PageView<News> pageView = new PageView<News>(20, formbean.getPage());
		QueryResult<News> qr = newsService.getScrollData(pageView
				.getFirstResult(), pageView.getMaxresult(), jpql.toString(),
				params.toArray(), orderby);
		pageView.setQueryResult(qr);
		return pageView;
	}

	/***
	 * 政策法规新闻
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param newsService
	 * @return
	 */
	public PageView<News> policyNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			NewsService newsService) {
		NewsForm formbean = (NewsForm) form;		
		StringBuffer jpql = new StringBuffer("");
		jpql.append(" o.feature.featureId = 3 and o.status = 'published'");
		List<Object> params = new ArrayList<Object>();
		
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("newsId", "desc");
		PageView<News> pageView = new PageView<News>(20, formbean.getPage());
		QueryResult<News> qr = newsService.getScrollData(pageView
				.getFirstResult(), pageView.getMaxresult(), jpql.toString(),
				params.toArray(), orderby);
		pageView.setQueryResult(qr);
		return pageView;
	}
	
	/**
	 * 修改点击次数
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void updateClick(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,NewsService newsService)
			throws Exception {
		NewsForm formbean = (NewsForm) form;
	    News news = new News();
	    news = newsService.find(formbean.getNewsId());
	    news.setClick(news.getClick()+ 1);
	    newsService.update(news);
	}
}
